package com.manager.cms.controller;

import com.manager.cms.annotation.IgnoreAuth;
import com.manager.cms.annotation.IgnoreLogin;
import com.manager.cms.bo.ExpressMan;
import com.manager.cms.bo.UserInfo;
import com.manager.cms.bo.UserRole;
import com.manager.cms.common.CommonResponse;
import com.manager.cms.common.Pageable;
import com.manager.cms.service.ExpressManService;
import com.manager.cms.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 
 *
 * @author 
 * @version v1.0
 * @date 2020-05-19
 */
@Slf4j
@RestController
@RequestMapping(value = "/expressMan")
public class ExpressManController {

    @Autowired
    private ExpressManService expressmanservice;

    @Autowired
    private UserRoleService userroleservice;

    @IgnoreAuth
    @IgnoreLogin
    @PostMapping("/register")
    public CommonResponse register(HttpServletRequest request, HttpServletResponse response, ExpressMan record) {
        try {
            ExpressMan query = new ExpressMan();
            query.setUsername(record.getUsername());
            List<ExpressMan> list = expressmanservice.getList(query);
            if (list.size() > 0) {
                return CommonResponse.fail("账号已存在");
            }
            record.setId(UUID.randomUUID().toString());
            record.setModifyDate(new Date());
            record.setCreateDate(new Date());
            expressmanservice.insert(record);

            UserRole userRole = new UserRole();
            userRole.setId(UUID.randomUUID().toString());
            userRole.setUserId(record.getId());
            userRole.setRole("express");
            userroleservice.insert(userRole);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/save")
    public CommonResponse save(HttpServletRequest request, HttpServletResponse response, ExpressMan record) {
        try {
            ExpressMan query = new ExpressMan();
            query.setUsername(record.getUsername());
            List<ExpressMan> list = expressmanservice.getList(query);
            if (list.size() > 0) {
                return CommonResponse.fail("账号已存在");
            }
            record.setId(UUID.randomUUID().toString());
            record.setModifyDate(new Date());
            record.setCreateDate(new Date());
            expressmanservice.insert(record);

            UserRole userRole = new UserRole();
            userRole.setId(UUID.randomUUID().toString());
            userRole.setUserId(record.getId());
            userRole.setRole("express");
            userroleservice.insert(userRole);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/update")
    public CommonResponse update(HttpServletRequest request, HttpServletResponse response, ExpressMan record) {
        try {
            ExpressMan expressMan = expressmanservice.selectByPrimaryKey(record.getId());
            record.setCreateDate(expressMan.getCreateDate());
            record.setModifyDate(new Date());
            expressmanservice.update(record);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/delete")
    public CommonResponse del(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            expressmanservice.del(id);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("删除失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/selectByPrimaryKey")
    public CommonResponse selectByPrimaryKey(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            ExpressMan info = expressmanservice.selectByPrimaryKey(id);
            return CommonResponse.succeed(info);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/list")
    public CommonResponse getList(HttpServletRequest request, HttpServletResponse response, ExpressMan record) {
        try {
            List<ExpressMan> list = expressmanservice.getList(record);
            return CommonResponse.succeed(list);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/pageList")
    public CommonResponse pageList(HttpServletRequest request, HttpServletResponse response, ExpressMan record,
                                   @RequestParam(value="pageNo",defaultValue = "1")Integer pageNo,
                                   @RequestParam(value="pageSize",defaultValue = "20")Integer pageSize) {
        try {
            Pageable<ExpressMan> page = expressmanservice.pageList(record,pageNo,pageSize);
            return CommonResponse.succeed(page);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }
}